from langgraph.prebuilt import create_react_agent
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from langgraph.checkpoint.memory import MemorySaver
from common.ali_qwen import get_model

# 初始化语言模型
model = get_model()

# 定义一个简单的工具来获取天气信息
@tool
def get_weather(city: str):
    """Use this tool to get the weather information for a city."""
    if city.lower() in ["上海", "北京"]:
        return f"{city}天气很好"
    elif city.lower() in ["广州", "深圳"]:
        return f"{city}天气糟糕"
    else:
        return "请查看中国天气网"

tools = [get_weather]
memory = MemorySaver()

agent = create_react_agent(model,tools=tools,checkpointer=memory)

#首次交互
config = {"configurable":{"thread_id":"zy_user"}}
inputs = {"messages": [("user", "北京的天气如何？简要回答，不需要额外信息")]}
response_1 = agent.invoke(inputs, config=config)
print(response_1['messages'][-1].content)

# 同一线程中的第二次交互 - 智能体记住上下文
inputs_2 = {"messages": [("user", "深圳的天气如何？")]}
response_2 = agent.invoke(inputs_2, config=config)
print(response_2['messages'][-1].content)